$(document)
		.ready(
				function() {
					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					if (totalCount == null || totalCount == ""
							|| totalCount == "undefined") {
						totalCount = 0;

					}
					if (pSize == null || pSize == "" || pSize == "undefined") {
						pSize = 10;

					}

					if (pageCount == null || pageCount == ""
							|| pageCount == undefined) {
						pageCount = 1;

					}

					$('#pagination2')
							.jqPaginator(
									{
										totalCounts:totalCount,
										pageSize : pSize,
										visiblePages : 5,
										currentPage : pageCount,

										prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {

											$("#pageIndex").val(num);
											var roleName = $("#roleName").val();

											var queryUrl = "queryByPage.do";
											$
													.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex : num,
															pageSize : pSize,
															roleName : roleName
														},
														cache : false,
														success : function(data) {

															$(
																	"#products tr:not(:first)")
																	.remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var role = results[i];

																var rowContent = "<tr>"
																		+ "<td><input name='proId' class='checkbox' type='checkbox' value='"
																		+ role.id
																		+ "'/></td>"
																		+ "<td>"
																		+ role.rCode
																		+ "</td>"
																		+ "<td>"
																		+ role.rName
																		+ "</td>"
																		+ "<td>"
																		+ role.note
																		+ "</td>"

																rowContent = rowContent
																		+ "<td><button name='editBtn' objId='"
																		+ role.id
																		+ "' class='btn btn-sm btn-primary'>修改</button></td>"
																		+ "<td><button name='viewUser' objId='"
																		+ role.id
																		+ "'"
																		+ "class='btn btn-sm btn-info'>查看用户列表</button></td>";
																rowContent = rowContent
																		+ "<td><button name='editAcc' objId='"
																rowContent = rowContent
																		+ role.id
																rowContent = rowContent
																		+ "'"
																rowContent = rowContent
																		+ "class='btn btn-sm btn-danger'>配置权限</button></td>"
																rowContent = rowContent
																		+ "</tr>";
																$("#products")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															alert("error"
																	+ html);
															return;
														}
													});
										}
									});

					$("#products").delegate('button[name="editAcc"]', 'click',
							function() {

								var roleId = $(this).attr("objId");
								var queryUrl = "queryAccByRole.do";
								var scopeVal = 1;

								var queryUrl = "queryAccByScope.do";
								$.ajax({
									url : queryUrl,
									dataType : 'json',
									data : {

										roleId : roleId,
										scope : scopeVal
									},
									cache : false,
									success : function(data) {
										$("#scopeLs").val(scopeVal);

										loadRoleAccess(data);
										$("#accDetailDialog").modal('show');
									},

									error : function(html) {
										alert("加载权限信息出错");
										return;
									}
								});

							});

					function loadRoleAccess(data) {

						var viewResLs = data.resList;
						var accls = data.accList;
						var role = data.role;
						if (role != null && role != undefined) {

							$("#accDetailDialog input[id='roleId']").val(
									role.id);
							$("#accDetailDialog input[id='rName']").val(
									role.rName);
							$("#accDetailDialog input[id='rCode']").val(
									role.rCode);
							$("#resList option").remove();

							$("#selectList option").remove();
							alert("--------------->1");

							if (viewResLs != null && viewResLs != undefined) {
								for (j = 0; j < viewResLs.length; j++) {
									var opt = "<option value='"
											+ viewResLs[j].id + "'>"
											+ viewResLs[j].vrName + "</option>";
									$("#resList").append(opt);
									
								}
								alert("--------------->2");
							}

							if (accls != null && accls != undefined) {
								for (i = 0; i < accls.length; i++) {
									var opt = "<option value='" + accls[i].id
											+ "'>" + accls[i].vrName
											+ "</option>";
									$("#selectList").append(opt);
								}
							}
							alert("--------------->3");

							
						} else {
							alert("加载权限信息出错");
						}

					}

					$("#products").delegate('button[name="viewUser"]', 'click',
							function() {
								// /$("accDetailDialog").modal('show');
							});

					$("#accDetailDialog").delegate("select[id='scopeLs']",
							"change", function() {
								var scopeVal = $(this).val();
								alert("scopeVal=" + scopeVal);
								if (scopeVal != null && scopeVal != undefined) {
									var queryUrl = "queryAccByScope.do";
									$.ajax({
										url : queryUrl,
										dataType : 'json',
										data : {

											roleId : roleId,
											scope : scopeVal
										},
										cache : false,
										success : function(data) {

											loadRoleAccess(data);
											

										},

										error : function(html) {
											alert("加载权限信息出错");
											return;
										}
									});
								}
							});

					

					$("#optBtn").delegate("button[id='saveBtn']", 'click', function() {
						$("#roleInfor").attr("method", "post");

						var proId = $("#roleInfor input[id='proId']").val();
						// alert(proId);
						if (proId == "" || proId == null
								|| proId == undefined) {
							
							$("#roleDetailDialog form[id='roleInfor']").attr("action","save.do");
							
						} else {
							$("#roleDetailDialog form[id='roleInfor']").attr("action","update.do");
						
						}
						alert($("#roleDetailDialog form[id='roleInfor']")==undefined);
						$("#roleDetailDialog form[id='roleInfor']").submit();
					});
					

					$("#accOpt")
							.delegate(
									"button[id='saveAccBtn']",
									'click',
									function() {
										
										if ($("#selectList").find("option").length > 0) {

											$("#selectList option").attr(
													"selected", "selected");
											$("#accDetail").attr("method","post");
											$("#accDetail").attr("action","saveAccInfor.do");
											$("#accDetail").submit();
										} else {
											alert("请选择需要分配的权限");
										}

									});

					/**
					 * 全选当前页记录
					 */
					$("#products").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='proId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#products").delegate('button[name="editBtn"]', 'click',
							function() {
								var roleId = $(this).attr("objId");

								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : roleId

									},
									cache : false,

									success : function(data) {

										loadRole(data);
									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										refresh();
									}
								});

							});

					$("#tools").delegate('#deleteBtn', 'click', function() {
						delChk = $('input[name="proId"]:checked');

						if (delChk.length > 0) {
							if (confirm("Are you sure delete the record?")) {

								var ids = "";
								/* var delUrl = "delete.do?ids="; */
								var delUrl = "delete.do";
								var idArr = new Array();

								for (var i = 0; i < delChk.length; i++) {
									idArr.push(delChk.get(i).value);

								}
								alert("---------->idArr "+idArr.toString());

								$.ajax({
									url : delUrl,
									
									dataType:"json",
									data : {
										ids : idArr.toString()
									},
									cache : false,

									success : function(html) {
										alert("Delete successfully");
										refresh();
									},
									error : function(html) {
										alert("Delete failly");
										refresh();
									}
								});
							} else {
								return;
							}
						} else {
							alert("Please select the record to delete");
							return;
						}
					});

					$("#rName")
							.delegate(
									$(this),
									'change',
									function() {

										var rName = $(this).val();

										queryUrl = encodeURI("queryRolePinYin.do?rName="
												+ rName + "");
										if (rName != null && rName != ""
												&& rName.length > 0) {
											$.ajax({
												url : queryUrl,
												dataType : 'text',

												cache : false,

												success : function(data) {

													$("#rCode").val(data)
												},
												error : function(data) {
													alert("获取角色编号出错");
												}
											});
										} else {
											alert("请输入角色名称");
										}
									});

					$("#tools").delegate('#addProduct', 'click', function() {

						var editUrl = "add.do";
						$.ajax({

							url : editUrl,
							dataType : 'json',

							cache : false,

							success : function(data) {

								// alert("************"+data.typeId);
								$("#roleDetailDialog input").each(function() {
									$(this).prop("value", null);
									$(this).prop("readonly", false);

								});
								$("#roleDetailDialog textarea").each(function() {
									$(this).prop("value", null);

								});

								loadRole(data)

							},
							error : function(html) {
								alert(html);
								alert("Load Data failly");
								refresh();
							}
						});

					});

					$("#addRes")
							.delegate(
									$(this),
									"click",
									function() {
										if ($("#resList").find(
												"option:selected").length > 0) {
											var maxId = $('#selectList option').length;
											if (maxId == null || maxId == ""
													|| maxId == undefined) {
												maxId = 0;
											}
											$("#resList")
													.find("option:selected")
													.each(
															function() {

																var itemId = $(
																		this)
																		.val();
																var itemName = $(
																		this)
																		.text();
																var selectOpt = "<option value='"
																		+ itemId
																		+ "'>"
																		+ itemName
																		+ "</option>";
																$("#selectList")
																		.append(
																				selectOpt);
																$("#resList option[value='"
																				+ itemId
																				+ "']")
																		.remove();
																maxId++;
															});
										} else {
											alert("请选择相关角色.");
										}
										isSelectRoleEmtyp();
									});

					$("#addAll")
							.delegate(
									$(this),
									"click",
									function() {

										var maxId = $('#selectList option').length;
										if (maxId == null || maxId == ""
												|| maxId == undefined) {
											maxId = 0;
										}
										if ($("#resList").find("option").length > 0) {

											$("#resList")
													.find("option")
													.each(
															function() {
																var itemId = $(
																		this)
																		.val();
																var itemName = $(
																		this)
																		.text();

																var selectOpt = "<option value='"
																		+ itemId
																		+ "'>"
																		+ itemName
																		+ "</option>";
																$("#selectList")
																		.append(
																				selectOpt);
																$(
																		"#resList option[value='"
																				+ itemId
																				+ "']")
																		.remove();
																maxId++;
															});
										} else {
											alert("请选择相关角色.");
										}
										
										isSelectRoleEmtyp();
									});

					$("#removeRes")
							.delegate(
									$(this),
									"click",
									function() {
										if ($("#selectList").find(
												"option:selected").length > 0) {
											$("#selectList")
													.find("option:selected")
													.each(
															function() {
																var itemId = $(
																		this)
																		.val();
																var itemName = $(
																		this)
																		.text();
																var selectOpt = "<option value="
																		+ itemId
																		+ ">"
																		+ itemName
																		+ "</option>";
																$("#resList")
																		.append(
																				selectOpt);
																$(
																		"#selectList option[value='"
																				+ itemId
																				+ "']")
																		.remove();
															});
										} else {
											alert("请选择相关角色.");
										}
										isSelectRoleEmtyp();
									});

					$("#removeAll")
							.delegate(
									$(this),
									"click",
									function() {
										if ($("#selectList").find("option").length > 0) {
											$("#selectList")
													.find("option")
													.each(
															function() {
																var itemId = $(
																		this)
																		.val();
																var itemName = $(
																		this)
																		.text();
																var selectOpt = "<option value="
																		+ itemId
																		+ " >"
																		+ itemName
																		+ "</option>";
																$("#resList")
																		.append(
																				selectOpt);
																$(
																		"#selectList option[value='"
																				+ itemId
																				+ "']")
																		.remove();
															});
										} else {
											alert("请选择相关角色.");
										}
										isSelectRoleEmtyp();

									});
					$("#resList")
							.delegate(
									'option',
									'dblclick',
									function() {
										if ($("#resList").find(
												"option:selected").length > 0) {
											var maxId = $('#selectList option').length;
											if (maxId == null || maxId == ""
													|| maxId == undefined) {
												maxId = 0;
											}
											$("#resList")
													.find("option:selected")
													.each(
															function() {

																var itemId = $(
																		this)
																		.val();
																var itemName = $(
																		this)
																		.text();
																var selectOpt = "<option value='"
																		+ itemId
																		+ "'>"
																		+ itemName
																		+ "</option>";
																$("#selectList")
																		.append(
																				selectOpt);
																$(
																		"#resList option[value='"
																				+ itemId
																				+ "']")
																		.remove();
																maxId++;
															});
										} else {
											alert("请选择相关角色.");
										}
										isSelectRoleEmtyp();

									});

					$("#selectList")
							.delegate(
									'option',
									'dblclick',
									function() {
										if ($("#selectList").find(
												"option:selected").length > 0) {
											$("#selectList")
													.find("option:selected")
													.each(
															function() {
																var itemId = $(
																		this)
																		.val();
																var itemName = $(
																		this)
																		.text();
																var selectOpt = "<option value="
																		+ itemId
																		+ ">"
																		+ itemName
																		+ "</option>";
																$("#resList")
																		.append(
																				selectOpt);
																$(
																		"#selectList option[value='"
																				+ itemId
																				+ "']")
																		.remove();
															});
										} else {
											alert("请选择相关角色.");
										}
										isSelectRoleEmtyp();

									});

					function loadRole(role) {

						$("#roleDetailDialog input[id='proId']").val(role.id);
						
						$("#roleDetailDialog input[id='rName']").val(role.rName);
						$("#roleDetailDialog input[id='rCode']").val(role.rCode);
						$("#roleDetailDialog textarea[id='note']").val(role.note);
						$("#roleDetailDialog").modal("show");
					}
					
					function isSelectRoleEmtyp(){
						
						if($("#selectList option").length<1)
							{
							$("#accOpt button[id='saveAccBtn']").attr("disabled","disabled");
							}else{
								$("#accOpt button[id='saveAccBtn']").removeAttr("disabled");
							}
					}

					function vaildate(form) {
						return true;
					}

					function refresh() {
						window.location.reload();
						$(".checkbox").removeAttr("checked");
					}
				});
